package com.hsy.bibc.modules.my.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.deparse.dpframe.utils.DPPicassoUtil;
import com.hsy.bibc.R;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.bibc.modules.my.gsondata.ShopStockGsonData;
import com.hsy.bibc.modules.my.gsondata.ShopStockGsonData;
import com.hsy.bibc.utils.CommonUtils;
import com.hsy.bibc.utils.HttpUtil;

import java.util.List;

/**
 * Created by Administrator on 2016/6/12.
 */
public class ShopStockActivity extends CommonBaseActivityWithSimpleActionBar {
    private ExpandableListView listView;
    private ShopStockGsonData.Result result = null;
    private GoodsListAdapter mGoodsListAdapter;
    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            result = DPGsonUtil.fromJson(response, ShopStockGsonData.Result.class);

            mGoodsListAdapter = new GoodsListAdapter(ShopStockActivity.this, result);
            listView.setAdapter(mGoodsListAdapter);

            if (result.getData().size() > 0) {
                for (int i = 0; i < result.getData().size(); i++) {

                    listView.expandGroup(0);
                }
            } else {
                DPLogUtil.d("listData.getWaste()==0");
            }
            CommonUtils.hideProgressDialog();
            getContentView().setVisibility(View.VISIBLE);
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onMsgError(int what, int type, String msg) {
            if (null == result) {

                DPLogUtil.d(msg);
                listView.setVisibility(View.GONE);

                empty_linear.inflate();

                getContentView().setVisibility(View.VISIBLE);
            }

            CommonUtils.hideProgressDialog();
        }
    };
    private ViewStub empty_linear;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle(R.string.title_my_shop_stock);
        setContentView(R.layout.activity_shop_stock);
        getContentView().setVisibility(View.GONE);

        listView = F(R.id.list_view);
        empty_linear = (ViewStub) findViewById(R.id.stub_import);
        listView.setGroupIndicator(null);

        CommonUtils.showProgressDialog(this);
        HttpUtil.httpGet(this, 0, Consts.Apis.GET_SHOP_MY_SHOP, callback);
    }

    public class GoodsListAdapter extends BaseExpandableListAdapter {
        private List<ShopStockGsonData.Data> listData;
        private LayoutInflater mInflater;

        public GoodsListAdapter(Context context, ShopStockGsonData.Result src) {
            listData = src.getData();
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        //得到子item需要关联的数据
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return (listData.get(childPosition).getChildren());
        }

        //得到子item的ID
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        //设置子item的组件
        @Override
        public View getChildView(final int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            ChildViewHolder childViewHolder = null;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_confirm_order_child, null);

                childViewHolder = new ChildViewHolder();
                childViewHolder.init(convertView);
            } else {
                childViewHolder = (ChildViewHolder) convertView.getTag();
            }
            childViewHolder.setData(listData.get(groupPosition).getChildren().get(childPosition));
            return convertView;
        }

        //获取当前父item下的子item的个数
        @Override
        public int getChildrenCount(int groupPosition) {
            return listData.get(groupPosition).getChildren().size();
        }

        //获取当前父item的数据
        @Override
        public Object getGroup(int groupPosition) {
            return listData.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return listData.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        //设置父item组件
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            convertView = mInflater.inflate(R.layout.item_confirm_order_group, null);
            TextView firstName = (TextView) convertView.findViewById(R.id.groupName);
            firstName.setText(listData.get(groupPosition).getName());

            ImageView parentRightImage = (ImageView) convertView.findViewById(R.id.parentRightImage);
            ImageView parentLeftImage = (ImageView) convertView.findViewById(R.id.parentLeftImage);

            DPPicassoUtil.with(ShopStockActivity.this).load(listData.get(groupPosition).getIcon()).into(parentLeftImage);
            if (isExpanded) {
                parentRightImage.setRotation(90);
            } else {
                parentRightImage.setRotation(0);
            }
            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public class ChildViewHolder {

            public ShopStockGsonData.Children src;

            public TextView nameTextView, score;
            public TextView priceTextView;
            public ImageView increaceImageView;
            public TextView valueEditTextExt;
            public ImageView decreaceImageView;
            public TextView unitTextView;
            public TextView showTextView;
            public LinearLayout editAreaLinearLayout;

            public ChildViewHolder() {

            }

            public void init(View convertView) {
                convertView.setTag(this);

                score = (TextView) convertView.findViewById(R.id.score);
                nameTextView = (TextView) convertView.findViewById(R.id.name);
                priceTextView = (TextView) convertView.findViewById(R.id.price);
                increaceImageView = (ImageView) convertView.findViewById(R.id.increace);
                valueEditTextExt = (TextView) convertView.findViewById(R.id.value);
                decreaceImageView = (ImageView) convertView.findViewById(R.id.decreace);
                unitTextView = (TextView) convertView.findViewById(R.id.unit);
                showTextView = (TextView) convertView.findViewById(R.id.show);
                editAreaLinearLayout = (LinearLayout) convertView.findViewById(R.id.edit_area);
            }

            public void setData(ShopStockGsonData.Children src) {
                if (null == src) {
                    DPLogUtil.e("src = null.");
                    return;
                }
                this.src = src;
                this.nameTextView.setText(src.getGoods_name());
//                this.score.setText(src.getScore() + "积分/个");
//                this.priceTextView.setText(src.getPrice() + "元" + "/" + src.getUnit());

                this.valueEditTextExt.setText("×" + String.valueOf(src.getQty()));
//                this.showTextView.setText(String.valueOf(src.getQty()));
            }
        }
    }
}
